package servlet;

import bean.User;
import service.UserService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Date;

/**
 * @Author : liuchangling
 * @Descrition :添加用户
 * @Date： Created in 1:39 下午 2021/5/28
 */
@WebServlet(name = "AddUserServlet", urlPatterns = "/add")
public class AddUserServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        User user = new User();
        int sno = Integer.parseInt(request.getParameter("sno"));
        user.setSno(sno);
        String name = new String(request.getParameter("name").getBytes("ISO-8859-1"), "UTF-8");
        user.setName(name);
        String sex = new String(request.getParameter("sex").getBytes("ISO-8859-1"), "UTF-8");
        if (sex.equals("男")) {
            user.setSex("male");
        } else {
            user.setSex("female");
        }
        String date = request.getParameter("birthday");
        user.setBirthday(date);
        String mail = request.getParameter("mail");
        user.setMail(mail);
        String remark = new String(request.getParameter("remark").getBytes("ISO-8859-1"), "UTF-8");
        user.setRemark(remark);
        int classNo=Integer.parseInt(request.getParameter("classNo").trim());
        user.setClassNo(classNo);
        //添加成功，重定向到添加成功的界面
        UserService userService = new UserService();
        boolean isSuccess = userService.addUserService(user);
        if (isSuccess == true) {
            response.sendRedirect("addAlert.jsp");
        }

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}
